import { Inject, Injectable } from '@nestjs/common';
import { CreateSoftDto } from './dto/create-soft.dto';
import { UpdateSoftDto } from './dto/update-soft.dto';
import { PrismaService } from 'src/common/prisma.service';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express'; // 假设你在 Nest.js 中使用 Express
@Injectable()
export class SoftService {
  constructor(
    private readonly prisma: PrismaService,
    @Inject(REQUEST) private req: Request,
  ) {}

  create(dto: CreateSoftDto) {
    return this.prisma.app.create({
      data: {
        ...dto,
        user: {
          connect: {
            id: (this.req as any).user.id,
          },
        },
      },
    });
  }

  async findAll(page: number = 1, pageSize: number = 10) {
    const skips = pageSize * (page - 1);
    const softs = await this.prisma.app.findMany({
      take: pageSize,
      skip: skips,
    });

    const total = await this.prisma.app.count();
    return { softs, total };
  }

  async findOne(id: number) {
    return this.prisma.app.findFirst({
      where: {
        id,
      },
    });
  }

  update(id: number, updateSoftDto: UpdateSoftDto) {
    return this.prisma.app.update({
      where: {
        id,
      },
      data: updateSoftDto,
    });
  }

  remove(id: number) {
    return this.prisma.app.deleteMany({
      where: {
        id,
      },
    });
  }
}
